﻿using System;
using System.Collections;
using UnityEngine;

namespace Nirvana
{
    public struct MaterialsEffect: IEquatable<MaterialsEffect>
    {

        internal Texture2D texture;
        internal ColorMode OverlayColorMode;
        internal float blurDistance;
        internal float overlaySpeed;
        /// <summary>
        /// 是否开启内切角
        /// </summary>
        internal bool isOpenInnerBevel;
        internal Color highlightColor;
        internal ColorMode highlightColorMode;
        internal Color shadowColor;
        internal ColorMode shadowColorMode;
        internal Vector2 highlightOffset;
        public int graylerp;

        public bool Equals(MaterialsEffect other)
        {
            return this.blurDistance == other.blurDistance && this.texture == other.texture
                && this.OverlayColorMode == other.OverlayColorMode
                && this.overlaySpeed == other.overlaySpeed
                && this.isOpenInnerBevel == other.isOpenInnerBevel
                && this.highlightColor == other.highlightColor
                && this.highlightColorMode == other.highlightColorMode
                && this.shadowColor == other.shadowColor
                && this.shadowColorMode == other.shadowColorMode
                && this.highlightOffset == other.highlightOffset
                && this.graylerp == other.graylerp;
        }

        public override int GetHashCode()
        {
            int num = this.blurDistance.GetHashCode();
            if (this.texture != null)
            {
                num = (397 * num ^ this.texture.GetHashCode());
            }
            num = (397 * num ^ this.OverlayColorMode.GetHashCode());
            num = (397 * num ^ this.overlaySpeed.GetHashCode());
            num = (397 * num ^ this.isOpenInnerBevel.GetHashCode());
            num = (397 * num ^ this.highlightColor.GetHashCode());
            num = (397 * num ^ this.highlightColorMode.GetHashCode());
            num = (397 * num ^ this.shadowColor.GetHashCode());
            num = (397 * num ^ this.shadowColorMode.GetHashCode());
            num = (397 * num ^ this.highlightOffset.GetHashCode());
            return 397 * num ^ this.graylerp.GetHashCode();
        }

        public Material CreateMaterial()
        {
            if (blurDistance == 0f && texture == null && !isOpenInnerBevel && graylerp == 0)
            {
                return null;
            }

            Shader shader = Shader.Find("UI/Effect");
            if (shader == null)
            {
                Debug.LogError("Can not found shader: 'UI/Effect'");
                return null;
            }
            Material material = new Material(shader);
            if (this.blurDistance > 0f)
            {
                material.EnableKeyword("UIEFFECT_BLUR");
                material.SetFloat("_BlurDistance", this.blurDistance);
            }


            if (this.texture != null)
            {
                if (this.overlaySpeed > 0f)
                {
                    material.EnableKeyword("UIEFFECT_OVERLAY_ANIMATION");
                    material.SetFloat("_OverlaySpeed", this.overlaySpeed);
                }
                else
                {
                    material.EnableKeyword("UIEFFECT_OVERLAY");
                }
                material.SetTexture("_OverlayTex", this.texture);
                material.SetInt("_OverlayColorMode", (int)this.OverlayColorMode);
            }

            if (this.isOpenInnerBevel)
            {
                material.EnableKeyword("UIEFFECT_INNER_BEVEL");
                material.SetColor("_HighlightColor", this.highlightColor);
                material.SetInt("_HighlightColorMode", (int)this.highlightColorMode);
                material.SetColor("_ShadowColor", this.shadowColor);
                material.SetInt("_ShadowColorMode", (int)this.shadowColorMode);
                material.SetVector("_HighlightOffset", this.highlightOffset);
            }

            if (this.graylerp > 0)
            {
                if (this.graylerp == 255)
                {
                    material.EnableKeyword("UIEFFECT_GRAYSCALE");
                }
                else
                {
                    material.EnableKeyword("UIEFFECT_GRAYSCALE_LERP");
                    material.SetFloat("_GrayLerp", 1f - (float)this.graylerp / 255f);
                }
            }
            return material;
        }
    };
}
